<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="接口名称" prop="apiName">
        <el-select
          v-model="queryParams.apiName"
          placeholder="请输入接口名称"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.aip_log_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="响应状态" prop="responseCode">
        <el-select
          v-model="queryParams.responseCode"
          placeholder="请输入响应状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.aip_log_res_code"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:apiLog:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="apiLogList" >
<!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="接口名称" align="center" prop="apiName" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.aip_log_type" :value="scope.row.apiName"/>
        </template>
      </el-table-column>
      <el-table-column label="接口地址" align="center" prop="apiUrl" />
      <el-table-column label="请求参数" align="center" prop="request" />
      <el-table-column label="响应状态" align="center" prop="responseCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.aip_log_res_code" :value="scope.row.responseCode"/>
        </template>
      </el-table-column>
      <el-table-column label="响应信息" align="center" prop="responseMsg" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listApiLog, getApiLog, delApiLog, addApiLog, updateApiLog } from "@/api/server/apiLog";

export default {
  name: "ApiLog",
  dicts: ['aip_log_type','aip_log_res_code'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接口使用日志表格数据
      apiLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apiName: null,
        apiUrl: null,
        responseCode: null,
        responseMsg: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询接口使用日志列表 */
    getList() {
      this.loading = true;
      listApiLog(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.apiLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        apiName: null,
        apiUrl: null,
        responseCode: null,
        responseMsg: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('server/apiLog/export', {
        ...this.addDateRange(this.queryParams, this.dateRange)
      }, `apiLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
